package com.daixu.xutil.demo;

import android.app.Activity;
import android.app.Application;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.daixu.xutil.demo.utils.CrashHandler;

import org.xutils.x;

import java.util.Stack;

/**
 * Created by yangbo on 2016/2/2.
 */
public class AppLoader extends Application {
    private static AppLoader mAppLoader;
    private static Stack<Activity> activityStack;
    public static int versionCode = 0;
    public static String versionName;
    public static String packageName;

    public static AppLoader getInstance() {
        return mAppLoader;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
        x.Ext.setDebug(true); // 是否输出debug日志

        mAppLoader = this;
        initVersionInfo();
        packageName = getPackageName();

//        CrashHandler handler = CrashHandler.getInstance();
//        handler.init(this);
    }

    /*******************************************************
     * 说明：初始化版本信息
     *
     * @author yangbo
     * @return
     ********************************************************/
    private void initVersionInfo() {
        PackageManager pm = getApplicationContext().getPackageManager();
        try {
            PackageInfo pi = pm.getPackageInfo(getPackageName(), 0);
            versionCode = pi.versionCode;
            versionName = pi.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * ****************************************************
     * 说明：Activity入栈
     *
     * @param activity ******************************************************
     * @author yangbo
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }

    /**
     * ****************************************************
     * 说明：取得最后入栈的Activity(当前Activity）
     *
     * @return ******************************************************
     * @author yangbo
     */
    public Activity currentActivity() {
        Activity activity = activityStack.lastElement();
        return activity;
    }

    /**
     * ****************************************************
     * 说明：结束最后入栈的Activity（当前Activity）
     *
     * @author yangbo
     * ******************************************************
     */
    public void finishActivity() {
        Activity activity = activityStack.lastElement();
        finishActivity(activity);
    }

    /**
     * ****************************************************
     * 说明：结束制定对象的Activity
     *
     * @param activity ******************************************************
     * @author yangbo
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            removeActivity(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * ****************************************************
     * 说明：结束掉指定类的Activity
     *
     * @param cls ******************************************************
     * @author yangbo
     */
    public void finishActivity(Class<?> cls) {
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
            }
        }
    }

    /**
     * ****************************************************
     * 说明：结束所有Activity
     *
     * @author yangbo
     * ******************************************************
     */
    public void finishAllActivity() {
        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (null != activityStack.get(i)) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }

    /**
     * ****************************************************
     * 说明：根据tags类名关闭activity
     *
     * @param tags ******************************************************
     * @author yangbo
     */
    public void removeActivitys(String tags) {
        for (Activity a : activityStack) {
            if (tags.contains(a.getClass().getSimpleName())) {
                a.finish();
            }
        }
    }

    /**
     * ****************************************************
     * 说明：移除制定对象的Activity
     *
     * @param activity ******************************************************
     * @author yangbo
     */
    public void removeActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
        }
    }

    /**
     * ****************************************************
     * 说明：退出应用程序
     *
     * @author yangbo
     * ******************************************************
     */
    public void exitApp() {
        try {
            finishAllActivity();
        } catch (Exception e) {
        }
    }
}
